package com.soul.dubbo.api.db;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soul.dubbo.api.mapper.UserInfoMapper;
import com.soul.model.db.UserInfo;
import com.soul.model.mongo.Video;
import com.soul.model.vo.PageResult;
import com.soul.model.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 首次登录---完善资料
     *
     * @param userInfo 对象
     */
    @Override
    public void add(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    /**
     * 更新用户信息
     *
     * @param userInfo 对象
     */
    @Override
    public void updateUserId(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 根据id查询用户
     *
     * @param userID id 可能是默认id
     * @return
     */
    @Override
    public UserInfoVo findIdUser(Long userID) {
        UserInfo userInfo = userInfoMapper.selectById(userID);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        userInfoVo.setAge(userInfo.getAge() + "");
        return userInfoVo;
    }

    /**
     * 查询黑名单用户
     *
     * @param blackListIds 集合黑名单用户id
     * @return
     */
    @Override
    public List<UserInfo> findByIdsUserInfoList(List<Long> blackListIds) {
        return userInfoMapper.selectBatchIds(blackListIds);
    }

    /**
     * 根据推荐用户id查询今日佳人用户
     *
     * @param userId 被推荐人用户id
     * @return
     */
    @Override
    public UserInfo findByUserIdUser(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    /**
     * @param ids 佳人用户ids
     * @return
     */
    @Override
    public Map<Long, UserInfo> findIdsUserInfoList(List<Long> ids) {
        List<UserInfo> userInfoList = userInfoMapper.selectBatchIds(ids);
        Map<Long, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, userInfo -> userInfo));
        return userInfoMap;
    }

    /**
     * 根据ids查询用户信息
     *
     * @param userIdsList ids集合
     * @return
     */
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIdsList) {
        List<UserInfo> userInfoList = userInfoMapper.selectBatchIds(userIdsList);
        return userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, userInfo -> userInfo));
    }

    /**
     * 查询ids集合并且有条件查询
     *
     * @param friendIdsList     ids集合
     * @param userInfoCondition 关键字
     * @return map
     */
    @Override
    public Map<Long, UserInfo> findByIdsAndCondition(List<Long> friendIdsList, UserInfo userInfoCondition) {
        // 1. lambda表达式条件查询
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserInfo::getId, friendIdsList);
        // 1.2 较空
        if (userInfoCondition != null) {
            if (StringUtils.isNotEmpty(userInfoCondition.getNickname())) {
                wrapper.like(UserInfo::getNickname, userInfoCondition.getNickname());
            }
            if (StringUtils.isNotEmpty(userInfoCondition.getGender())) {
                wrapper.eq(UserInfo::getGender, userInfoCondition.getGender());
            }
            if (userInfoCondition.getAge() != null) {
                wrapper.le(UserInfo::getAge, userInfoCondition.getAge());
            }
        }
        // 2. 执行查询
        List<UserInfo> userInfoList = userInfoMapper.selectList(wrapper);
        Map<Long, UserInfo> userInfoMap = new HashMap<>();
        // 3. userInfoList集合转为map对象
        if (!CollectionUtils.isEmpty(userInfoList)) {
            userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, userInfo -> userInfo));
        }
        return userInfoMap;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findPage(Long page, Long pageSize) {
        // 分页查询，没条件
        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(new Page<>(page, pageSize), null);
        // 构建分页结果返回
        return new PageResult(page, pageSize, userInfoPage.getTotal(), userInfoPage.getRecords());
    }

    /**
     * 条件查询用户ids
     *
     * @param userInfo
     * @return
     */
    @Override
    public List<UserInfo> findNicknameUser(UserInfo userInfo,Long page,Long pagesize) {
        LambdaQueryWrapper<UserInfo> query = new LambdaQueryWrapper<>();
        if (userInfo != null) {
            if (StringUtils.isNotEmpty(userInfo.getNickname())) {
                query.like(userInfo.getNickname() != null, UserInfo::getNickname, userInfo.getNickname());
            }
        }
        Page<UserInfo> userInfoPage = new Page<>((page-2),pagesize);
        userInfoMapper.selectPage(userInfoPage, query);
        List<UserInfo> records = userInfoPage.getRecords();
        return records;
    }

    /**
     * 查询用户总条数
     *
     * @return
     */
    @Override
    public Integer findUserCounts() {
        Wrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        return userInfoMapper.selectCount(queryWrapper);
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @Override
    public List<UserInfo> findUserInfoAll() {
        return userInfoMapper.selectList(new QueryWrapper<>());
    }

/*    @Override
    public PageResult findByUserId(Long page, Long pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").in(userId));
        long count = mongoTemplate.count(query, Video.class);
        query.limit(pagesize.intValue()).skip((page -1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Video> list = mongoTemplate.find(query, Video.class);
        return new PageResult(page,pagesize,count,list);
    }*/
}
